// pages/address/phone/phone.js

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    second: 60,
    send: false,
    mobile: '',
    mobileVal: ''
  },
  //获取验证码
  mobileVal: function (e) {
    mobileVal = e.detail.value
    // console.log(e)
  },
  input: function (e) {
    this.setData({ mobileVal: e.detail.value });
  },
  countdown: function () {
    var nsecond = 60;
    var that = this;
    that.setData({
      send: true
    });

    var mobileVal = this.data.mobileVal;
    var rooturl = this.data.rooturl;
    this.configmobile(mobileVal);
    //获取验证码
    wx.request({
      url: rooturl + 'member_tele.php?type=sms',
      data: {
        mobile: mobileVal
      },
      header: { 'content-type': 'application/json' },
      method: 'POST',
      dataType: 'json',
      success: (result) => {

      },
    });

    var appCount = setInterval(function () {
      nsecond -= 1;
      that.setData({
        second: nsecond
      })
      if (nsecond < 1) {
        clearInterval(appCount);
        //取消指定的setInterval函数将要执行的代码 
        that.setData({
          second: 60,
          send: false
        })
      }
      // console.log(111);
    }, 1000);
  },
  //清空手机号
  empty: function (e) {
    this.setData({
      mobile: ''
    })
  },
  //表单提交
  formSubmit: function (e) {
    console.log(e);
    var mobile = e.detail.value.mobile;
    var code = e.detail.value.code;
    this.configmobile(mobile);
    if (code == '') {
      wx.showToast({
        title: '请填写验证码',
        icon: 'clear',
        duration: 1500
      })
      return false;
    } else if (code == ) {

    }
    wx.request({
      url: rooturl + 'member_tele.php?type=add',
      data: {

      },
      header: { 'content-type': 'application/json' },
      method: 'POST',
      dataType: 'json',
      success: (result) => {

      }

    })
  },

  //验证手机号
  configmobile: function (mobileVal) {
    var regular = /0?(13|14|15|17|18|19)[0-9]{9}/;
    if (mobileVal == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'clear',
        duration: 1500
      })
      return false;
    } else if (!(regular.test(mobileVal))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'clear',
        duration: 1500
      })
      return false;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var rooturl = app.globalData.url;
    var that = this;

    wx.request({
      url: rooturl + 'member_tele.php',
      header: { 'content-type': 'application/json' },
      method: 'POST',
      dataType: 'json',
      success: (result) => {
        that.setData({
          mobileVal: result.data.result.data,
          rooturl: rooturl
        })
      },
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})